package com.gome.ai.aiparter.ws.test;


import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Scanner;

@Slf4j
public class WebClient extends WebSocketClient {

	public WebClient(URI serverUri) {
		super(serverUri);
	}

	public static void main(String[] args) throws URISyntaxException {
		URI uri = new URI("ws://127.0.0.1:11720/ws/yma");
		new WebClient(uri).connect();
//		while (true){
//			try {
//				new WebClient(uri).connect();
//			}catch (Exception e){
//				log.error("创建连接失败,3s后重试:{}",uri,e);
//			}
//
//			try {
//				Thread.sleep(3000);
//			} catch (InterruptedException e) {
//				throw new RuntimeException(e);
//			}
//		}
	}

	@Override
	public void onOpen(ServerHandshake serverHandshake) {
		log.info("连接到服务器");

		Scanner scanner = new Scanner(System.in);
		new Thread(() -> {
			while (true){
				String line = scanner.nextLine();
				send(line);
			}
		}).start();
	}

	@Override
	public void onMessage(String s) {
		System.out.print(s);
	}

	@Override
	public void onClose(int i, String s, boolean b) {
		log.info("客户端关闭");
	}

	@Override
	public void onError(Exception e) {
		log.warn("客户端发送错误",e);
	}
}
